package com.solarfridge.monitor.shared;

import java.util.Date;

import com.googlecode.gchart.client.GChart;

public class DataChart extends GChart {

	public static class DateStampedValue {
		Date date;
		double value;
		public DateStampedValue(long timeSinceEpoch, double value) {
			this.date = new Date(timeSinceEpoch);
			this.value = value;
		}
	}
	
	public static class DataSet {
		final String name;
		final DateStampedValue[] data;
		
		public DataSet(String name, DateStampedValue[] data){
			this.name = name;
			this.data = data;
		}
	}
	
	{	//Initialization
		setChartTitle("Fridge Temperatures");
		getXAxis().setTickCount(6);
		getXAxis().setTickLabelFormat("=(Date)dd/h:mm a");
		getXAxis().setAxisLabel("<small>Date</small>");
		setChartSize(400, DEFAULT_Y_CHARTSIZE);
		setOptimizeForMemory(true);
		getYAxis().setAxisLabel("<small>T (&deg;C)</small>");
	}
	
	public DataChart(DataSet ... dataSets) {
		for(DataSet set : dataSets) {
			addTempCurve(set);
		}
	}
	
	/**
	 * Adds a curve with the proper formating
	 */
	private void addTempCurve(DataSet set){
		addCurve();
		getCurve().getSymbol().setFillThickness(2);
		getCurve().getSymbol().setFillSpacing(10);
		getCurve().setLegendLabel(set.name);
		
		for (int i = 0; i < set.data.length; i++){
			getCurve().addPoint(set.data[i].date.getTime(), set.data[i].value);
		}
	}

}
